<!-- 分类字典 -->
<template>
  <div class="app-container">
    <!-- 搜索聊天记录   -->
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="host" label="域名">
          <el-input
            v-model="queryParams.host"
            placeholder="host"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item prop="ip" label="ip">
          <el-input
            v-model="queryParams.ip"
            placeholder="ip"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 规则列表   -->
    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="handleOpenDialog()">
          <i-ep-plus />
          新增
        </el-button>
      </template>

      <el-table
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
      >
        <el-table-column label="ID" prop="id" />
        <el-table-column label="客服ID" prop="kefuId" />
        <el-table-column label="客服名称" prop="kefuName" />
        <el-table-column label="域名" prop="host" />
        <el-table-column label="域名匹配" prop="hostMatch">
          <template #default="scope">
            {{ scope.row.hostMatch == 1 ? "模糊匹配" : "精准匹配" }}
          </template>
        </el-table-column>
        <el-table-column label="域名使用" prop="hostUse">
          <template #default="scope">
            {{ scope.row.hostUse == 1 ? "使用" : "停用" }}
          </template>
        </el-table-column>
        <el-table-column label="IP" prop="ip" />
        <el-table-column label="IP匹配" prop="ipMatch">
          <template #default="scope">
            {{ scope.row.ipMatch == 1 ? "模糊匹配" : "精准匹配" }}
          </template>
        </el-table-column>
        <el-table-column label="IP使用" prop="ipUse">
          <template #default="scope">
            {{ scope.row.ipUse == 1 ? "使用" : "停用" }}
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort" />

        <el-table-column label="操作" fixed="right" align="left" width="150">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click.stop="handleOpenDialog(scope.row.id)"
            >
              <i-ep-delete />
              编辑
            </el-button>
            <el-button
              type="danger"
              link
              size="small"
              @click.stop="handleDelete(scope.row)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 规则表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form
        ref="roleFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="选择客服" prop="kefuId">
          <el-select v-model="formData.kefuId">
            <template v-for="option in userOption" :key="option.id">
              <el-option :label="option.username" :value="option.id" />
            </template>
          </el-select>
        </el-form-item>

        <el-form-item label="域名" prop="host">
          <el-input v-model="formData.host" placeholder="请输入域名" />
        </el-form-item>

        <el-form-item label="域名匹配" prop="hostMatch">
          <el-radio-group v-model="formData.hostMatch">
            <el-radio :label="1">模糊匹配</el-radio>
            <el-radio :label="0">精准匹配</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="域名使用" prop="hostUse">
          <el-radio-group v-model="formData.hostUse">
            <el-radio :label="1">使用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="IP" prop="ip">
          <el-input v-model="formData.ip" placeholder="请输入IP" />
        </el-form-item>

        <el-form-item label="IP匹配" prop="ipMatch">
          <el-radio-group v-model="formData.ipMatch">
            <el-radio :label="1">模糊匹配</el-radio>
            <el-radio :label="0">精准匹配</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="IP使用" prop="ipUse">
          <el-radio-group v-model="formData.ipUse">
            <el-radio :label="1">使用</el-radio>
            <el-radio :label="0">停用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="formData.sort"
            controls-position="right"
            :min="0"
            style="width: 100px"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="js">
defineOptions({
  name: "Rule",
  inherititems: false,
});
import { deleteRule, getPage, getRule, updateRule, saveRule } from "@/api/rule";
import UserAPI from "@/api/user";

const userOption = ref([]);

getUserOption();

function getUserOption() {
  UserAPI.getOption().then((data) => {
    userOption.value = data;
  });
}

const queryFormRef = ref(ElForm);

const loading = ref(false);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  host: "",
  ip: "",
});
// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

/** 打开规则弹窗 */
function handleOpenDialog(ruleId) {
  dialog.visible = true;
  if (ruleId) {
    dialog.title = "修改规则";
    let data = { id: ruleId };
    getRule(data).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增规则";
  }
}

const roleFormRef = ref(ElForm);
// 规则表单
const formData = reactive({
  id: 0,
  kefuId: "",
  kefuName: "",
  host: "",
  hostMatch: 1,
  hostUse: 1,
  ip: "",
  ipMatch: 1,
  ipUse: 1,
  sort: 1,
});

const rules = reactive({
  kefuId: [{ required: true, message: "请选择客服", trigger: "blur" }],
  host: [{ required: true, message: "请输入域名", trigger: "blur" }],
  hostMatch: [
    { required: true, message: "请选择域名匹配方式", trigger: "blur" },
  ],
  hostUse: [{ required: true, message: "请选择是否使用", trigger: "blur" }],
  ip: [{ required: true, message: "请输入ip", trigger: "blur" }],
  ipMatch: [{ required: true, message: "请选择ip匹配方式", trigger: "blur" }],
  ipUse: [{ required: true, message: "请选择是否使用", trigger: "blur" }],
  sort: [{ required: true, message: "请指定排序", trigger: "blur" }],
});

/** 关闭角色弹窗 */
function handleCloseDialog() {
  dialog.visible = false;

  roleFormRef.value.resetFields();
  roleFormRef.value.clearValidate();

  formData.id = undefined;
  formData.sort = 1;
}

/** 提交规则表单 */
function handleSubmit() {
  roleFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      const roleId = formData.id;
      if (roleId) {
        updateRule(formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        saveRule(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

// 聊天记录表格数据
const pageData = ref([]);

/** 查询 */
function handleQuery() {
  loading.value = true;
  getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.count;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleDelete(row) {
  loading.value = true;
  let d = { id: row.id };
  deleteRule(d)
    .then((data) => {
      handleQuery();
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});
</script>

<style scoped lang="scss"></style>
